﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MVCClubSite.IDAL;
using MVCClubSite.Model;
using MVCClubSite.BLL;
using MVCClubSite.Web.ViewData;
using MVCClubSite.Helpers;

namespace MVCClubSite.Web.Controllers
{
    public class LocationsController : BaseController
    {
        EventsService _eventsService;
        LocationsService _locationsService;

        public LocationsController()
            : this(null, null)
        {
        }

        public LocationsController(EventsService eventsService, LocationsService locationsService)
        {
            _eventsService = eventsService ?? new EventsService();
            _locationsService = locationsService ?? new LocationsService();
        }

        [Authorize(Roles = "Administrator")]
        [AcceptVerbs("GET")]
        public ActionResult List()
        {
            LocationListViewData data = new LocationListViewData();
            data.Locations = _locationsService.GetLocations();

            return View("List", data);
        }

        [AcceptVerbs("GET")]
        public ActionResult Details(string id)
        {
            int locationID = int.Parse(id);
            LocationViewData data = new LocationViewData();

            data.Location = _locationsService.GetLocationByID(locationID);

            return View("Details", data);
        }

        [Authorize(Roles = "Administrator")]
        [AcceptVerbs("GET")]
        public ActionResult New()
        {
            return View("New");
        }

        [Authorize(Roles = "Administrator")]
        [AcceptVerbs("POST")]
        public ActionResult Create(FormCollection form)
        {
            Location newLocation = new Location();
            this.UpdateModel(newLocation, new[] { "Title", "Description", "Url", "ImageID", "Address" });
            _locationsService.Save(newLocation);

            return RedirectToAction("List");
        }

        [Authorize(Roles = "Administrator")]
        [AcceptVerbs("GET")]
        public ActionResult Edit(string id)
        {
            int locationID = int.Parse(id);
            LocationViewData data = new LocationViewData();

            data.Location = _locationsService.GetLocationByID(locationID);
            return View("Edit", data);
        }

        [Authorize(Roles = "Administrator")]
        [AcceptVerbs("POST")]
        public ActionResult Update(FormCollection form, int id)
        {
            Location existingLocation = new Location();
            this.UpdateModel(existingLocation, new[] { "Title", "Description", "Url", "ImageID", "Address" });
            _locationsService.Save(existingLocation);

            return RedirectToAction("List");
        }
    }
}
